<template>
  <div>
    <van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
>
<comment-item @click-reply="$emit('click-reply',$event)" v-for="item in list" :key="item.com_id+''" :obj="item" ></comment-item>
</van-list>
  </div>
</template>

<script>
import { getCommentList } from '@/api/article'
import CommentItem from './CommentItem.vue'
export default {
  components: { CommentItem },
  data () {
    return {

      loading: false,
      finished: false,
      limit: 10,
      offset: null
    }
  },
  props: {
    list: {
      type: Array,
      default: () => []
    },
    type: {
      type: String,
      default: 'a'
    },
    source: {
      type: [String, Number, Object],
      required: true
    }
  },

  created () {

  },

  methods: {
    async  onLoad () {
      const { results, last_id: lastid, total_count: totalcount } = await getCommentList({
        type: this.type,
        source: this.source,
        offset: this.offset,
        limit: this.limit
      })
      this.$emit('on-success', totalcount)
      this.list.push(...results)
      this.loading = false
      if (!results.length) {
        this.finished = true
      } else {
        this.offset = lastid
      }

      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
    }

  }
}
</script>

<style scoped lang='less'>

</style>
